<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>聚城网络科技有限公司</title>
    <style>
        :root {
            --primary-color: #5b8cff;
            --user-bg: linear-gradient(135deg, #5b8cff 0%, #3d6ef7 100%);
            --bot-bg: linear-gradient(135deg, #f0f8ff 0%, #e6f3ff 100%);
            --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        }

        body {
            font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
            margin: 0;
            padding: 20px;
            display: flex;
            justify-content: center;
            min-height: 100vh;
            background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
        }

        .chat-container {
            width: 100%;
            max-width: 800px;
            height: 90vh;
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            box-shadow: var(--shadow);
            backdrop-filter: blur(10px);
            display: flex;
            flex-direction: column;
            overflow: hidden;
        }

        .chat-header {
            padding: 24px;
            background: var(--primary-color);
            color: white;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        }

        .chat-header h1 {
            margin: 0;
            font-size: 1.8rem;
            font-weight: 600;
            letter-spacing: -0.5px;
        }

        .chat-messages {
            flex: 1;
            padding: 20px;
            overflow-y: auto;
            display: flex;
            flex-direction: column;
            gap: 12px;
        }

        .chat-message {
            max-width: 75%;
            padding: 16px 20px;
            border-radius: 20px;
            line-height: 1.5;
            animation: messageAppear 0.3s ease-out;
            position: relative;
            word-break: break-word;
        }

        .chat-message.user {
            background: var(--user-bg);
            color: white;
            align-self: flex-end;
            border-bottom-right-radius: 4px;
            box-shadow: var(--shadow);
        }

        .chat-message.bot {
            background: var(--bot-bg);
            color: #2d3748;
            align-self: flex-start;
            border-bottom-left-radius: 4px;
            box-shadow: var(--shadow);
        }

        .chat-input {
            padding: 20px;
            background: rgba(255, 255, 255, 0.9);
            border-top: 1px solid rgba(0, 0, 0, 0.05);
            display: flex;
            gap: 12px;
        }

        .chat-input input {
            flex: 1;
            padding: 14px 20px;
            border: 2px solid rgba(0, 0, 0, 0.1);
            border-radius: 16px;
            font-size: 1rem;
            transition: all 0.2s ease;
            background: rgba(255, 255, 255, 0.8);
        }

        .chat-input input:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(91, 140, 255, 0.2);
        }

        .chat-input button {
            padding: 12px 24px;
            border: none;
            border-radius: 16px;
            background: var(--primary-color);
            color: white;
            font-size: 1rem;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.2s ease;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .chat-input button:hover {
            background: #406cff;
            transform: translateY(-1px);
        }

        .chat-input button:disabled {
            background: #c2d1ff;
            cursor: not-allowed;
            transform: none;
        }

        .chat-input button svg {
            width: 18px;
            height: 18px;
            fill: currentColor;
        }

        @keyframes messageAppear {
            from {
                opacity: 0;
                transform: translateY(10px);
            }

            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .typing-indicator {
            display: inline-flex;
            gap: 6px;
            padding: 12px 20px;
            background: var(--bot-bg);
            border-radius: 20px;
            align-self: flex-start;
        }

        .typing-dot {
            width: 8px;
            height: 8px;
            background: rgba(0, 0, 0, 0.3);
            border-radius: 50%;
            animation: typing 1.4s infinite ease-in-out;
        }

        .typing-dot:nth-child(2) {
            animation-delay: 0.2s;
        }

        .typing-dot:nth-child(3) {
            animation-delay: 0.4s;
        }

        @keyframes typing {

            0%,
            100% {
                transform: translateY(0);
            }

            50% {
                transform: translateY(-4px);
            }
        }

        @media (max-width: 640px) {
            body {
                padding: 10px;
            }

            .chat-container {
                height: 95vh;
                border-radius: 16px;
            }

            .chat-message {
                max-width: 85%;
            }
        }
    </style>
</head>

<body>
    <div class="chat-container">
        <div class="chat-header">
            <h1>聚城智能助手</h1>
        </div>
        <div class="chat-messages" id="chatMessages"></div>
        <div class="chat-input">
            <input type="text" id="questionInput" placeholder="输入消息..." onkeydown="handleKeyDown(event)">
            <button id="sendButton" disabled>
                <svg viewBox="0 0 24 24">
                    <path d="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z" />
                </svg>
                发送
            </button>
        </div>
    </div>

    <script>
        const questionInput = document.getElementById('questionInput');
        const sendButton = document.getElementById('sendButton');
        const chatMessages = document.getElementById('chatMessages');
        let isBotResponding = false;

        // 输入验证
        questionInput.addEventListener('input', () => {
            sendButton.disabled = questionInput.value.trim().length === 0;
        });

        // 回车发送
        function handleKeyDown(e) {
            if (e.key === 'Enter' && !sendButton.disabled && !isBotResponding) {
                sendButton.click();
            }
        }

        // 修改后的消息处理逻辑
        let currentBotMessage = null; // 当前正在更新的AI消息

        async function handleBotResponse(response) {
            const reader = response.body.getReader();
            const decoder = new TextDecoder();
            let buffer = '';

            currentBotMessage = displayMessage('bot', '');

            try {
                while (true) {
                    const { done, value } = await reader.read();
                    if (done) {
                        // 处理最后剩余的数据
                        if (buffer) processLine(buffer);
                        break;
                    }

                    buffer += decoder.decode(value, { stream: true });
                    const lines = buffer.split('\n');

                    // 保留未完成的行在缓冲区
                    buffer = lines.pop() || '';

                    lines.forEach(line => {
                        if (line.startsWith('data:')) {
                            const data = line.replace(/^data:\s*/g, '').trim();
                            if (data === '[DONE]') return;
                            currentBotMessage.innerHTML += data;
                        }
                    });

                    chatMessages.scrollTop = chatMessages.scrollHeight;
                }
            } finally {
                currentBotMessage = null;
            }
        }
        // 发送逻辑
        sendButton.addEventListener('click', async () => {
            if (isBotResponding) return;

            const question = questionInput.value.trim();
            if (!question) return;

            questionInput.value = '';
            sendButton.disabled = true;
            isBotResponding = true;

            // 显示用户消息（新消息始终出现在下方）
            displayMessage('user', question);

            try {
                // 显示加载状态
                const typingIndicator = createTypingIndicator();

                const response = await fetch('/deepSeek/chat', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Accept': 'text/event-stream'
                    },
                    body: JSON.stringify({ question }),
                });

                typingIndicator.remove();
                await handleBotResponse(response); // 处理流式响应

            } catch (error) {
                displayMessage('bot', '暂时无法处理您的请求，请稍后再试');
            } finally {
                isBotResponding = false;
                questionInput.focus();
            }
        });

        // 创建消息元素
        function displayMessage(sender, content) {
            const messageDiv = document.createElement('div');
            messageDiv.className = `chat-message ${sender}`;
            messageDiv.textContent = content;
            chatMessages.appendChild(messageDiv);
            chatMessages.scrollTop = chatMessages.scrollHeight;
            return messageDiv;
        }

        // 创建输入指示器
        function createTypingIndicator() {
            const container = document.createElement('div');
            container.className = 'typing-indicator';
            container.innerHTML = `
                <div class="typing-dot"></div>
                <div class="typing-dot"></div>
                <div class="typing-dot"></div>
            `;
            chatMessages.appendChild(container);
            chatMessages.scrollTop = chatMessages.scrollHeight;
            return container;
        }

        // 更新 AI 消息
        // let currentBotMessage = null;
        // function updateBotMessage(content) {
        //     if (!currentBotMessage) {
        //         currentBotMessage = displayMessage('bot', content);
        //     } else {
        //         currentBotMessage.textContent = content;
        //     }
        //     chatMessages.scrollTop = chatMessages.scrollHeight;
        // }
    </script>
</body>

</html>